package _回溯;

import java.util.ArrayList;
import java.util.List;

public class _17_电话号码的字符组合 {

    final String[] latterMap = {"", "", "abc", "def", "hig", "jkl", "mno", "pqrs", "tuv", "wxyz"};
    List<String> res = new ArrayList<>();
    StringBuilder path = new StringBuilder();
    // 17 ：电话号码的字符组合：https://leetcode.cn/problems/letter-combinations-of-a-phone-number/
    public List<String> letterCombinations(String digits) {
        if (digits.length() == 0)
            return res;
        back(digits, 0);
        return res;
    }

    private void back(String digits, int index) {
        if (digits.length() == index) {
            res.add(path.toString());
            return;
        }
        // 找到要遍历的数字对应的字符串，
        int digit = digits.charAt(index) - '0';
        String s = latterMap[digit];
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            path.append(c);
            back(digits, index + 1);
            path.deleteCharAt(path.length() - 1);
        }
    }


}
